package com.hmh.student.students.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmh.student.students.entity.Teacher;
import com.hmh.student.students.service.ITeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xyf
 * @since 2025-04-16
 */
@RestController
@RequestMapping("/teacher")
public class TeacherController {
    @Autowired
    private ITeacherService teacherService;

    @GetMapping("/getTeachers")
    public List<Teacher> getTeachers() {
        List<Teacher> teacherList = teacherService.list();
        return teacherList;
    }

    // 修改教师信息
    @PutMapping("/updateTeacher")
    public Map<String, Object> updateTeacher(@RequestBody Teacher teacher) {
        Map<String, Object> map = new HashMap<>();
        try {
            teacherService.updateById(teacher);
            map.put("msg", "success");
        } catch (Exception e) {
            map.put("msg", "更新失败: " + e.getMessage());
        }
        return map;
    }

    // 添加教师
    @PostMapping("/addTeacher")
    public Map<String, Object> addTeacher(@RequestBody Teacher teacher) {
        Map<String, Object> map = new HashMap<>();
        try {
            teacherService.save(teacher);
            map.put("msg", "success");
        } catch (Exception e) {
            map.put("msg", "添加失败");
        }
        return map;
    }

    // 删除教师
    @DeleteMapping("/deleteTeacher")
    public Map<String, Object> deleteTeacher(@RequestParam Integer teacherNo) {
        Map<String, Object> map = new HashMap<>();
        try {
            teacherService.removeById(teacherNo);
            map.put("msg", "success");
        } catch (Exception e) {
            map.put("msg", "删除失败");
        }
        return map;
    }

    // 分页查询教师
    @GetMapping("/pageTeacher")
    public Page<Teacher> pageTeacher(@RequestParam(required = false) String name,
                                     @RequestParam(required = false) String department,
                                     @RequestParam(defaultValue = "10") Integer pageSize,
                                     @RequestParam(defaultValue = "1") Integer current) {
        return teacherService.queryTeacherPage(name, department, pageSize, current);
    }
}
